package com.lizhangjun.nim.server;

import com.lizhangjun.nim.constant.*;
import com.lizhangjun.nim.session.SessionManager;
import io.netty.handler.timeout.IdleStateEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.yeauty.annotation.*;
import org.yeauty.pojo.Session;

/**
 * 单播服务器
 * @author lizhangjun
 */
@Slf4j
@Component
@ServerEndpoint(port = "8181", path = "/v1/im/unicast/{userId}")
public class UnicastServer
{
    @BeforeHandshake
    public void handshake(Session session){
        session.setSubprotocols("stomp");
    }

    @OnOpen
    public void onOpen(Session session, @PathVariable String userId){
        try {
            SessionManager.add(null,userId, session);
            String connect = new IMessage.Builder(Flag.OK)
                    .mineType(MineType.TEXT.getIndex())
                    .chatType(ChatType.UNICAST.getIndex())
                    .msgType(MsgType.HANDSHAKE.getIndex())
                    .content("连接成功")
                    .buildStr();
            session.sendText(connect);
        } catch (Exception e) {
            String connect = new IMessage.Builder(Flag.FAIL)
                    .mineType(MineType.TEXT.getIndex())
                    .chatType(ChatType.UNICAST.getIndex())
                    .msgType(MsgType.HANDSHAKE.getIndex())
                    .content("连接失败")
                    .buildStr();
            session.sendText(connect);
            log.error("UnicastServer-onOpen异常：", e);
        }
    }

    @OnClose
    public void onClose(Session session,  @PathVariable String userId) {
        try {
            SessionManager.remove(userId, session);
            String close = new IMessage.Builder(Flag.OK)
                    .mineType(MineType.TEXT.getIndex())
                    .chatType(ChatType.UNICAST.getIndex())
                    .msgType(MsgType.EXIT.getIndex())
                    .content("离线")
                    .buildStr();
            session.sendText(close);
        }catch (Exception e){
            log.error("UnicastServer-onClose异常：", e);
        }
    }

    @OnError
    public void onError(Session session, @PathVariable String userId) {
        try {
            SessionManager.remove(userId, session);
            String error = new IMessage.Builder(Flag.OK)
                    .mineType(MineType.TEXT.getIndex())
                    .chatType(ChatType.UNICAST.getIndex())
                    .msgType(MsgType.CLOSE.getIndex())
                    .content("强制下线")
                    .buildStr();
            session.sendText(error);
        }catch (Exception e){
            log.error("UnicastServer-onError异常：", e);
        }
    }

    @OnMessage
    public void onMessage(Session session) {
        try {
            String heartbeat = new IMessage.Builder(Flag.OK)
                    .mineType(MineType.TEXT.getIndex())
                    .chatType(ChatType.UNICAST.getIndex())
                    .msgType(MsgType.HEARTBEAT.getIndex())
                    .content("pong")
                    .buildStr();
            session.sendText(heartbeat);
        }catch (Exception e){
            log.error("UnicastServer-onMessage异常：", e);
        }
    }

    @OnEvent
    public void onEvent(Session session, Object evt) {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            switch (idleStateEvent.state()) {
                case READER_IDLE:
                    System.out.println("客户端进入读空闲");
                    break;
                case WRITER_IDLE:
                    System.out.println("客户端进入写空闲");
                    break;
                case ALL_IDLE:
                    session.close();
                    System.out.println("客户端进入读写空闲");
                    break;
                default:
                    break;
            }
        }
    }
}
